import Common from '../../../../common/common.js';
const app = getApp();
let common = new Common;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //卡券列表
    listArray: [],
    userList:[],
    userListMore:[],
    //是否iphone x
    ipx: '',
    //提示窗
    showModal: false,
    rodate:0,//判断显示生命
    page:1,
    memberList:[],//客户列表
    couponId:'',//卡券id
    couponCnt:''//发放卡券数量
  },
  toDetail:function(e){
    console.log(e)
    let rodate = e.currentTarget.dataset.rodate;
    if (rodate==0){
      this.setData({
        rodate: 1
      })
    }else{
      this.setData({
        rodate: 0
      })
    }
 
  },
  changCouponsNum:function(e){
    console.log(e)
    let index = e.currentTarget.dataset.index;
    let listArray = this.data.listArray;
    console.log(listArray[index].couponsNum);
    listArray[index].couponsNum=e.detail.value;
    this.setData({
      couponCnt: e.detail.value
    })
    console.log(this.data.listArray)
  },
  /**
   * 选择卡券
   */
  radioChange: function (e) {
    var valueArray = e.detail.value;
    console.log(e)
   this.setData({
     couponId: valueArray  
   })
    let listArray = this.data.listArray;
    for (var i = 0; i < listArray.length;i++){
      if (listArray[i].uuid != valueArray){
        listArray[i].couponsNum = '';
      }
    }
   this.setData({
     couponCnt:'',
     listArray: listArray
   })
   
  },
  show:function(){
    this.setData({
      showModal: true
    })
  },
  /**
   * 取消弹窗
   */
  cancleModel: function () {
    this.setData({
      showModal: false
    })
  },
  getList:function(page,type){
    console.log(this.data.memberList)
    let data = {
      condition: {
        'orgSeq': this.data.orgSeq,
        memberList: this.data.memberList
      },
      current: this.data.page,
      size: 10,
    }
    common.requestAjax('/api/promotion-service/v1/promotionCoupon/queryDxCouponForMemberList', 'POST', data).then(res => {
      if (res.data.code == 1) {
        let list = res.data.records;
        if (type == 1) {
          //下拉刷新
          list = res.data.records;
        } else if (type == 2) {
          //上拉加载
          list = list.concat(res.data.records);
        }
        this.setData({
          listArray: list
        })
        console.log(this.data.listArray)
   
      }

    }).catch(err => {
      console.log(err);
    })
  },
  // 获取用户列表
  getUserList:function(e){
    let members = this.data.memberList;
    members = '('+JSON.stringify(members.toString())+')'
    console.log(members)
    let data = {
      orgSeq: '',
      memberList: this.data.memberList,
      keywords: '',
      memberName: '',
      memberTel: ''
    }
    common.requestAjax('/api/member-service/v1/memberTrade/queryMemberListByParam', 'POST', data).then(res => {  
      this.setData({ 
        userListMore:res.data,
      }) 
      if(res.data.length < 3){
        this.setData({
          userList:res.data
        })
      }else{
        let arr = res.data.slice(0,2); //截取数组对象的前两个值
        this.setData({
          userList:arr, 
        })
      } 
    }).catch(err => {
      console.log(err);
    })
  },
  // 确认给用户发放卡券
  upDownManage:function(e){
    if (!this.data.couponId){
        wx.showToast({
          title: '请选择卡券',
          icon:'none'
        })
        return;
    }
    if (!this.data.couponCnt) {
      wx.showToast({
        title: '请输入卡券数量',
        icon: 'none'
      })
      return;
    }
    let that = this;
    wx.showModal({
      title: '提示',
      content: '请再次确认卡券信息',
      success:function(res){
        if (res.confirm) {
          that.saveMemberCoupon();
        } else if (res.cancel) {
        }
      }
    })
  },
  saveMemberCoupon:function(e){
    let data = {
      orgSeq: this.data.orgSeq,
      memberList: this.data.memberList,
      couponId: this.data.couponId,
      couponCnt: this.data.couponCnt
    }
    common.requestAjax('/api/promotion-service/v1/promotionCoupon/saveMemberCoupon', 'POST', data).then(res => {
      if (res.data.code == 1) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',//图标，支持"success"、"loading" 
        })
        this.getList();
        this.setData({
          couponId:''
        })
        // setTimeout(()=>{
        //   wx.redirectTo({
        //     url: '/pages/couponsMgt/couponsIssue/index',
        //   })
        // },2000)
      }

    }).catch(err => {
      console.log(err);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let orgSeq = app.globalData.orgSeq;
    let ipx = app.globalData.isIphoneX;
    let memberList = options.memberList.split(",")
   
    this.setData({
      ipx: ipx,
      orgSeq: orgSeq,
      memberList: memberList
    });
    console.log(options)
    this.getList();
    this.getUserList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getList(1, 1);
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1200)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page;
    page = page + 1;
    this.getList(page, 2);
    this.setData({
      page: page
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})